package com.hkfn.rfb.common.http;

import android.content.Context;

import com.hkfn.rfb.common.bean.UserInfoModel;

import java.util.HashMap;

import okhttp3.FormBody;
import okhttp3.RequestBody;

public class Url {
    public static String HTTP = "http://";
    public static String URL_CONTENT = "182.92.190.160:8848";
    //public static String URL_CONTENT = "192.168.47.1:8848";
    public static String REQUEST_HOST = HTTP + URL_CONTENT;
    public static final String DEVICE = "/app";

    public static String IMAGE_PATH = HTTP + URL_CONTENT;

    public static String TCP_IP_PATH = "101.37.193.214";
    public static String TCP_PORT = "8088";

    /**
     * 上传数据
     */
    public static String DATA_REPORT_URL = REQUEST_HOST + "/blade-app-change/changeStationTest/dataReport";

    /**
     * 获取电池箱参数数据
     */
    public static String GET_BATTERY_INFO_URL = REQUEST_HOST + "/blade-app-change/changeStationTest/dataReport";

    /**
     * 获得Get的请求Url
     * @param context
     * @param url
     * @param params params 请求参数
     * @return
     */
    public static String getGetUrl(Context context, String url, HashMap<String, String> params) {
        StringBuilder sb = new StringBuilder(url);
        if (url.contains("?")) {
            sb.append("&");
        } else {
            sb.append("?");
        }
        params = getRequestParams(context, params);

        for (String key : params.keySet()) {
            sb.append(key);
            sb.append("=");
            sb.append(params.get(key));
            sb.append("&");
        }
        return sb.substring(0, sb.length() - 1);
    }

    public static HashMap<String, String> getRequestParams(Context context,
                                                           HashMap<String, String> params) {
        if (null == params){
            params = new HashMap<>();
        }
//        params.put("f", "android");
//        // 版本code
//        params.put("code", Utils.getVersionCode(context) + "");
//        // 版本
//        params.put("v", Utils.getVersionName(context));
        params.put("token", UserInfoModel.getUserToken(context));
        return params;
    }


    public static String getPostParamsStr(HashMap<String, String> params) {
        StringBuilder sb = new StringBuilder();
        for (String key : params.keySet()) {
            sb.append(key);
            sb.append("=");
            sb.append(params.get(key));
            sb.append("&");
        }
        return sb.substring(0, sb.length() - 1);
    }

    public static RequestBody getPostRequestBody(HashMap<String, String> params) {
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : params.keySet()) {
            builder.add(key, params.get(key));
        }
        return builder.build();
    }
}
